package listeners;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JTextPane;

import view.Block;
import view.DrawPanel;
import view.IfBlock;
import view.Line;
import view.PictureModel;

public class LineMouseListener extends MouseAdapter {
	private DrawPanel paint;
	private PictureModel pictureModel;
	private JTextPane textPane;
	private ArrayList<JButton> buttons;
	private ArrayList<JButton> toolbarButtons;
	private SightMouseMotionListener sightListener;
	private Line line = null;
	private GeneralPath path;
	private boolean gotFirst = false;
	private int blockCount;

	public LineMouseListener(DrawPanel paint, ArrayList<JButton> buttons,ArrayList<JButton> toolbarButtons,
			PictureModel pictureModel, JTextPane textPane) {
		this.paint = paint;
		this.buttons = buttons;
		this.toolbarButtons = toolbarButtons;
		this.pictureModel = pictureModel;
		this.textPane = textPane;
	}

	@Override
	public void mousePressed(MouseEvent event) {
		Point point = event.getPoint();
		textPane.setText("");
		// get points between blocks
		if (gotFirst == true) {
			//level point to Line.width
			Point calculate = Line.calculatePoint(point, Line.previous);
			Line.previous = calculate;
			if(pictureModel.getBlockByControlPoint(point) == null){
				path.lineTo(calculate.x, calculate.y);
			}			
			//sigth listener action
			if(sightListener != null){
				paint.removeMouseMotionListener(sightListener);
				paint.clear();
				pictureModel.draw();
				pictureModel.drawControlPoints();
				paint.repaint();
			}
			sightListener = new SightMouseMotionListener(paint,pictureModel,path);
			disableAllButtons(toolbarButtons);
			paint.addMouseMotionListener(sightListener);
		}
		
		//press on block`s control points
		Block block = pictureModel.getBlockByControlPoint(point);
		if (block == null) {
			//set previous point
			if(path != null){
				line.setSecondLast(Line.previous);
			}
			return;
		}
		//block counter
		blockCount++;

		//press on first block
		if (gotFirst == false) {
			//block isCausePoint
			if(!block.isCausePoint(point)){
				textPane.setText("It`s not cause poit!!!");
				return;
			}
			paint.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
			gotFirst = true;
			line = new Line();
			line.setCauseBlock(block);
			//set type of line
			if(block instanceof IfBlock){
				IfBlock ifBlock = (IfBlock) block;
				ifBlock.setLineType(point, line);
			}
			path = new GeneralPath();
			Point control = block.getControlPointByClosePoint(point);			
			Line.previous = control;
			line.setSecondLast(Line.previous);
			path.moveTo(control.x, control.y);
			disableAllButtons(buttons);
		}
		
		//press on second block
		if (blockCount == 2) {
			releaseAllButtons(toolbarButtons);
			paint.removeMouseMotionListener(sightListener);
			line.setSecondBlock(block);
			
			Point calculate = Line.calculatePoint(point, Line.previous);
			Point control = block.getControlPointByClosePoint(point);
			
			if(block.getControlPointByClosePoint(calculate) != null){
				line.setLast(calculate);
				path.lineTo(calculate.x, calculate.y);
			}else{
				line.setLast(control);
				path.lineTo(control.x, control.y);
			}	
			
			gotFirst = false;
			paint.setCursor(Cursor.getDefaultCursor());
		}				
	}

	@Override
	public void mouseReleased(MouseEvent event) {
		if(path != null){
			paint.getGraphics2D().setColor(Color.BLACK);
			paint.getGraphics2D().draw(path);			
			if(blockCount == 2){
				blockCount = 0;
				Line.drawArrow(paint.getGraphics2D(), line.getSecondLast().x, line.getSecondLast().y, line.getLast().x, line.getLast().y);			
				line.setPath(path);
				pictureModel.addLine(line);
				path = null;
				releaseAllButtons(buttons);
			}
			paint.repaint();
		}

	}
	
	public void setPath(GeneralPath path){
		this.path = path; 
	}
	private void releaseAllButtons(ArrayList<JButton> buttons) {
		for (JButton b : buttons) {
			b.setEnabled(true);
		}
	}
	private void disableAllButtons(ArrayList<JButton> buttons){
		for (JButton b : buttons) {
			b.setEnabled(false);
		}
	}

}
